package rmmk.ntp4;

import java.util.ArrayList;
import java.util.List;

public class CalculateCost implements ICalculateCost {

	@Override
	public int calculate(List<CanoeRentalPoint> crpList) {
		int totalCost = 0;
		
		ArrayList<Integer> rentalPointNrs = new ArrayList<>();
		
		for(Integer i = 0; i < crpList.size() - 1; ++i)
		{
			if(rentalPointNrs.isEmpty())
			{
				rentalPointNrs.add(i);
				totalCost += crpList.get(i).getCost();
				continue;
			}
			
			CanoeRentalPoint lastChoosen = crpList.get(rentalPointNrs.get(rentalPointNrs.size() - 1));
			CanoeRentalPoint current = crpList.get(i);
			if(current.getCost() < lastChoosen.getCost())
			{
				rentalPointNrs.add(i);
				totalCost += current.getCost();
			} else totalCost += lastChoosen.getCost();
		}
		
		System.out.println("Koszt całkowity: " + totalCost);
		printPoints(crpList, rentalPointNrs);
		
		return totalCost;
	}
	
	private void printPoints(List<CanoeRentalPoint> crpList, ArrayList<Integer> rentalPointNrs)
	{
		System.out.print("Punkty: [");
		
		for(Integer pointNr : rentalPointNrs)
		{
			System.out.print("(" + pointNr + ", " + crpList.get(pointNr).getCost() + ")");
		}
		
		System.out.println("]");
	}

}
